<?php

namespace App\Console\Commands;

use JonnyW\PhantomJs\Client;
use QL\QueryList;
use Illuminate\Support\Facades\DB;
use Illuminate\Console\Command;

class DigColumn extends Command
{
	//php artisan dig:ceshi 151 http://www.woshipm.com
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dig:column {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'dig:column';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action   = $this->argument('action');
        if ($action == 'chinaz')
        {
            $this->info('开始运行！！！请等待');
            $data   = $this->check('https://top.chinaz.com');
            if($data){
            	$out = preg_replace("'<script[^>]*?>.*?</script>'si", '', $data);
    	    	$html = QueryList::html($out)->find('.TopNav-MenuItemThree .MenuItemCent');
    	        $ql = QueryList::html($html->html())->rules([
    	            //采集规则
    	            'link'  => ['a','href'],
    	            'title' => ['a','text'],
    	        ])->range('.MenuItem-SubList li')->query()->getData(function ($item){
    	        	$this->info($item['title'].'开始采集！！！');
    				$rt = QueryList::get('https://top.chinaz.com'.$item['link']);
    				$arrayRows = $rt->find('.TopListCent-Head .HeadFilter')->map(function($row){
    				    return [
    				        $row->find('a')->texts()->all(),
    				        $row->find('a')->attrs('href')->all()
    				    ];
    				});
    				$item['array']	= $arrayRows->all()[0][0];
				    $item['link']   = $arrayRows->all()[0][1];
    				$id = DB::table('columns')->insertGetId([
                        'parent_id'		=> 0,
                        'type'			=> 0,
                        'order'			=> 0,
                        'title'			=> $item['title'],
                        'created_at'    => date('Y-m-d H:i:s', time()),
                        'updated_at'    => date('Y-m-d H:i:s', time()),
                    ]);
                    if ($id)
                    {
                    	foreach($item['array'] as $kk => $vv)
                    	{
                    		$pid = DB::table('columns')->insertGetId([
    		                    'parent_id'		=> $id,
    		                    'type'			=> 0,
    		                    'order'			=> 0,
    		                    'title'			=> $vv,
    		                    'created_at'    => date('Y-m-d H:i:s', time()),
    		                    'updated_at'    => date('Y-m-d H:i:s', time()),
    		                ]);
    		                if ($pid)
    		                {
    		                    DB::table('crontabs')->insertGetId([
        		                    'type'		    => 'shell',
        		                    'title'			=> '采集'. $vv .'网站',
        		                    'contents'		=> 'php '.base_path().'/artisan dig:site '. $pid .' https://top.chinaz.com'. $item['link'][$kk],
        		                    'schedule'		=> '10 0 * * *',
        		                    'sleep'			=> 0,
        		                    'maximums'		=> 0,
        		                    'executes'		=> 0,
        		                    'begin_at'		=> date('Y-m-d H:i:s', time()),
        		                    'end_at'		=> '2999-01-20 13:24:54',
        		                    'execute_at'	=> date('Y-m-d H:i:s', time()),
        		                    'weigh'			=> 100,
        		                    'status'		=> 'normal',
        		                    'created_at'    => date('Y-m-d H:i:s', time()),
        		                    'updated_at'    => date('Y-m-d H:i:s', time()),
        		                ]);
    		                }
    		                $this->info($vv .'==pid=='. $pid .'==link=='. $item['link'][$kk]);
                    	}
                    }
                    $this->info($item['title'].'结束采集！！！');
    	            return $item;
    	        });
    	        $data   = $ql->all();
    	        $this->info('结束！！！');
            }else{
                $this->error('dig article error!');
            }
        }
    	if ($action == 'aizhan')
        {
            $this->info('开始运行！！！请等待');
            $data   = $this->check('https://top.aizhan.com');
            if($data){
                $html = QueryList::html($data)->find('.header .subnav div:eq(0) .d0 dd ul');
    	        $ql = QueryList::html($html->html())->rules([
    	            //采集规则
    	            'link'  => ['a','href'],
    	            'title' => ['a','text'],
    	        ])->range('li')->query()->getData(function ($item){
    	        	$this->info($item['title'].'开始采集！！！');
    				$rt = QueryList::get($item['link']);
    				$arrayRows = $rt->find('.wlist .main .clearfix .fr .cate .item ul')->map(function($row){
    				    return [
    				        $row->find('li a')->texts()->all(),
    				        $row->find('li a')->attrs('href')->all()
    				    ];
    				});
    				$item['array']	= $arrayRows->all()[0][0];
				    $item['link']   = $arrayRows->all()[0][1];
    				$id = DB::table('columns')->insertGetId([
                        'parent_id'		=> 0,
                        'type'			=> 0,
                        'order'			=> 0,
                        'title'			=> $item['title'],
                        'created_at'    => date('Y-m-d H:i:s', time()),
                        'updated_at'    => date('Y-m-d H:i:s', time()),
                    ]);
                    if ($id)
                    {
                    	foreach($item['array'] as $kk => $vv)
                    	{
                    		$pid = DB::table('columns')->insertGetId([
    		                    'parent_id'		=> $id,
    		                    'type'			=> 0,
    		                    'order'			=> 0,
    		                    'title'			=> $vv,
    		                    'created_at'    => date('Y-m-d H:i:s', time()),
    		                    'updated_at'    => date('Y-m-d H:i:s', time()),
    		                ]);
    		                if ($pid)
    		                {
    		                    DB::table('crontabs')->insertGetId([
        		                    'type'		    => 'shell',
        		                    'title'			=> '采集'. $vv .'网站',
        		                    'contents'		=> 'php '.base_path().'/artisan dig:siteaizhan '. $pid .' '. $item['link'][$kk],
        		                    'schedule'		=> '10 0 * * *',
        		                    'sleep'			=> 0,
        		                    'maximums'		=> 0,
        		                    'executes'		=> 0,
        		                    'begin_at'		=> date('Y-m-d H:i:s', time()),
        		                    'end_at'		=> '2999-01-20 13:24:54',
        		                    'execute_at'	=> date('Y-m-d H:i:s', time()),
        		                    'weigh'			=> 100,
        		                    'status'		=> 'normal',
        		                    'created_at'    => date('Y-m-d H:i:s', time()),
        		                    'updated_at'    => date('Y-m-d H:i:s', time()),
        		                ]);
    		                }
    		                $this->info($vv .'==pid=='. $pid .'==link=='. $item['link'][$kk]);
                    	}
                    }
                    $this->info($item['title'].'结束采集！！！');
                    sleep(3);
    	            return $item;
    	        });
    	        $data   = $ql->all();
    	        $this->info('结束！！！');
            }else{
                $this->error('dig article error!');
            }
        }
    }	
    
    //抓取网址
    function check($url){
        
        $client = Client::getInstance();
        $client->isLazy(); // Tells the client to wait for all resources before rendering
        /** 
         * @see JonnyW\PhantomJs\Http\Request
         **/
        $request = $client->getMessageFactory()->createRequest($url, 'GET');
        $request->setTimeout(5000); // Will render page if this timeout is reached and resources haven't finished loading
        $request->setDelay(5);
    
        /** 
         * @see JonnyW\PhantomJs\Http\Response 
         **/
        $response = $client->getMessageFactory()->createResponse();
    
        // Send the request
        $client->send($request, $response);
    
        if($response->getStatus() === 200) {
            $html   = $response->getContent();
            $html    = preg_replace("/<[ ]+/si","<", $html); //过滤<("<"号后面带空格)
            $html    = preg_replace("/<\!–.*?–>/si","", $html); //注释
    		$html    = preg_replace("'<script[^>]*?>.*?</script>'si", '', $html);
    		$html    = preg_replace("/<(\/?head.*?)>/si", "", $html); //过滤head标签
    		$html    = preg_replace("/<(\/?meta.*?)>/si", "", $html); //过滤meta标签
            $html    = preg_replace("/<(\/?body.*?)>/si", "", $html); //过滤body标签
            $html    = preg_replace("/<(\/?link.*?)>/si", "", $html); //过滤link标签
            $html    = preg_replace("'<style[^>]*?>.*?</style>'si", '', $html);
            $html    = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","", $html); //过滤title标签
            $html    = preg_replace("/<(\/?title.*?)>/si","", $html); //过滤title标签
            $html   = preg_replace("'<script[^>]*?>.*?</script>'si", '', $html);
            // Dump the requested page content
            return $html;
        } else {
            return false;
        }
        
    }
}
